package com.beta.support;

import com.beta.dto.order.OrderItemDto;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by vector on 2017/2/6.
 */
public abstract class OrderItemCollector {

    public static BigDecimal getPrice(List<? extends OrderItemDto> orderItems) {
        if (CollectionUtils.isEmpty(orderItems)) {
            return BigDecimal.ZERO;
        }

        return orderItems.stream()
                .map(OrderItemDto::getPrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static BigDecimal getWeight(List<? extends OrderItemDto> orderItems) {
        if (CollectionUtils.isEmpty(orderItems)) {
            return BigDecimal.ZERO;
        }

        return orderItems.stream()
                .map(orderItem -> new BigDecimal(orderItem.getQuantity()).multiply(orderItem.getUnitWeight()))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static BigDecimal getFreight(List<? extends OrderItemDto> orderItems) {
        if (CollectionUtils.isEmpty(orderItems)) {
            return BigDecimal.ZERO;
        }

        return orderItems.stream()
                .map(orderItem -> BigDecimal.valueOf(orderItem.getQuantity())
                        .multiply(orderItem.getUnitWeight())
                        .multiply(orderItem.getUnitFreight()))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static BigDecimal getAmount(List<? extends OrderItemDto> orderItems) {
        return getPrice(orderItems).add(getFreight(orderItems));
    }

    public static Integer getQuantity(List<? extends OrderItemDto> orderItems) {
        if (CollectionUtils.isEmpty(orderItems)) {
            return 0;
        }

        return orderItems.stream()
                .collect(Collectors.summingInt(OrderItemDto::getQuantity));
    }
    
}
